import pymysql
import traceback

def get_from_databse(user_id):
    try:
        db = pymysql.connect(host="localhost", user="root", password="157352asd", database="ktsys", charset="utf8")
        print("数据库连接成功")
    except pymysql.Error as e:
        print("数据库连接失败：" + str(e))
    # 使用cursor()方法获取操作游标
    cursor = db.cursor()
    # SQL 查询语句
    sql1 = "select count(order) from skill_builder_data_corrected_csv where user_id=%s"  # 统计做题数量
    print(sql1)
    try:
        # 执行sql语句 数组传参
        cursor.execute(sql1, user_id)
        exercise_num = cursor.fetchone()[0]#学生做题数量
    except:
        # 如果发生错误则回滚
        traceback.print_exc()
        db.rollback()
    db.close()
    return exercise_num

# get_from_databse(64525)

def generate_select_name_from_content():
    try:
        db = pymysql.connect(host="localhost", user="root", password="157352asd", database="ktsys", charset="utf8")
        print("数据库连接成功")
    except pymysql.Error as e:
        print("数据库连接失败：" + str(e))
    # 使用cursor()方法获取操作游标
    cursor = db.cursor()
    # SQL 查询语句
    sql1 = "select content from exercise_select"  # 统计做题数量
    print(sql1)
    try:
        # 执行sql语句 数组传参
        cursor.execute(sql1)
        exercise_num = cursor.fetchone()[0]#学生做题数量
    except:
        # 如果发生错误则回滚
        traceback.print_exc()
        db.rollback()
    db.close()
    return exercise_num

get_from_databse(64525)